<?php
namespace app\admin\controller;

use think\Controller;
use think\Env;
use think\response\View;
use think\Validate;

class {subFolder}Controller extends Base
{

[folder start]
    //region   [tblTitle]模快
    //列表查询
    public function {tblName}list()
    {
        if(request()->isPost()){
            //$post = request()->post();
            $pagesize=request()->post("pagesize");
            $pageindex=request()->post("pageindex");

            //查询条件
            $cond=[];
				[loop start]
            $[fldName]=request()->post("[fldName]");
            if(strlen($[fldName])>0){
                $cond["[fldName]"]=["=",$[fldName]];
            }
				[loop end]^
            //print_r($cond);die; 

            //分页总数
            $total=db("{tblName}")->where($cond)->count();
            $data["total"]=$total;

            //数据列表
            $rows=db("{tblName}")->where($cond)
				->field("{strNames}")
				->page($pageindex,$pagesize)->select();
            $data["rows"]=$rows;
            $this->success($data);
        }
        return view();
    }

   //获取
    public function {tblName}view()
    {
        if(request()->isPost()) {
            $id = request()->post("id");
            if ($id == 0) {
                //表单初始值
                $data["id"] = 0;
                //$data["status"]=1;
                $this->success($data);
            }else {
                $model = db('{tblName}')->where("id", $id)->find();
                if ($model) {
                    $this->success($model);
                } else {
                    $this->error("记录获取失败");
                }
            }
        }
        return view();
    }

 
    //添加
    public function {tblName}edit()
    {
        if (request()->isPost()) {
            /* {strNames} */
            $post = request()->post();
            $id=request()->post("id");
            // 验证规则
            $validate = new Validate([
  [loop start]
                "[fldName]"  => "require",
  [loop end]^

			],[
  [loop start]
                "[fldName].require"=>"[fldTitle]不能为空",
  [loop end]^

            ]);

			$da=$post;
            if($id) {
                if(!$validate->check($post)){
                    $this->validateError($validate->getError());
                }
				//修改保存
                db('{tblName}')->where("id",$post["id"])->update($da);
            }else{
                if(!$validate->check($post)){
                    $this->validateError($validate->getError());
                }
				//添加保存
  [loop start]
				//$da["[fldName]"]=[fldDefaultValue];
  [loop end]^

                db('{tblName}')->insert($da);
            }
            $this->success($post);
        }

        return view();
    }

    //删除
    public function  {tblName}delete()
    {
        $id=request()->post("id");
        if(!$id){
            $this->error("非法的参数");
        }
        $result=db('{tblName}')->delete($id);
        if($result === false){
            $this->error("删除失败");
        }else{
            $this->success($id);
        }
    }
    //endregion

[folder end]	
}
